package com.oreilly.ez.service.impl;

import com.oreilly.ez.entity.EzCate;
import com.oreilly.ez.repo.EzCateRepo;
import com.oreilly.ez.service.EzCateService;
import com.oreilly.ez.util.CloneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class EzCateServiceImpl  implements EzCateService{


    @Autowired
    private EzCateRepo ezCateRepo;

    @Override
    public EzCate save(EzCate item) {

        // is update ??
        Integer sid = item.getSid();
        if(sid != null){
            EzCate dbItem = ezCateRepo.getOne(sid);

            CloneUtils.copyProperties(item, dbItem);
            CloneUtils.copyProperties(dbItem, item);

        }

        return ezCateRepo.save(item);

    }

    @Override
    public void remove(Integer sid) {

        ezCateRepo.deleteById(sid);
    }


    @Override
    public Page<EzCate> find(EzCate item, Pageable pageable) {

        return ezCateRepo.findAll(pageable);
    }

    @Override
    public Object findAll() {
        return ezCateRepo.findAll();
    }

    @Override
    public EzCate findOne(Integer sid) {

        return ezCateRepo.findById(sid).get();
    }


}
